package com.yidu.best.stock.stockTrade.service.impl;

import java.io.InputStream;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.yidu.best.basicSetup.exchange.util.IDUtil;
import com.yidu.best.stock.moneyTransfers.dao.MoneyTransfersDao;
import com.yidu.best.stock.moneyTransfers.domain.MoneyTransfers;
import com.yidu.best.stock.stockTrade.dao.StockTradeDao;
import com.yidu.best.stock.stockTrade.domain.StockTrade;
import com.yidu.best.stock.stockTrade.domain.StockTradeVo;
import com.yidu.best.stock.stockTrade.service.StockTradeService;
import com.yidu.best.stock.stockTrade.util.ExcelUtil;
import com.yidu.best.systemManagement.userControl.util.IDGenerator;
@Service
public class StockTradeServiceImpl implements StockTradeService{

	 @Autowired
	private StockTradeDao stockTradeDao;
	 @Autowired
	 private MoneyTransfersDao moneyTransfersDao;
	@Override
	public Map<String, Object> queryStockPlateByCriteriaWithPage(StockTradeVo stockTradeVo) {
		 int page = (stockTradeVo.getPage()-1)*stockTradeVo.getLimit(); stockTradeVo.setPage(page);
			List<StockTrade> pageByParam = stockTradeDao.queryStockPlateByCriteriaWithPage(stockTradeVo);
			System.out.println(pageByParam);
			int count = stockTradeDao.selectCount(stockTradeVo);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("data",pageByParam);
			map.put("count", count);
			map.put("msg", "");
			map.put("code", 0);
			return map;
	}

	@Override
	public int selectCount(StockTradeVo stockTradeVo) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void ajaxUploadExcel(InputStream in, MultipartFile file) throws Exception {
		List<List<Object>> listob = ExcelUtil.getBankListByExcel(in, file.getOriginalFilename());
		List<StockTrade> stockTradeList = new ArrayList<StockTrade>();
		for(int i=0;i<listob.size();i++){
			try{
				List<Object> ob = listob.get(i);
				StockTrade stockTrade = new StockTrade();
				stockTrade.setStockTradeId(IDUtil.getUUID());
				stockTrade.setStockTradeNo("ZJJY-"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) +"-" + IDUtil.getUUIDA());
				stockTrade.setFundId(String.valueOf(ob.get(0)));
				stockTrade.setFundCode(String.valueOf(ob.get(1)));
				stockTrade.setFundName(String.valueOf(ob.get(2)));
				stockTrade.setStockId(String.valueOf(ob.get(3)));
				stockTrade.setStockCode(String.valueOf(ob.get(4)));
				stockTrade.setStockName(String.valueOf(ob.get(5)));
				stockTrade.setUserId(String.valueOf(ob.get(6)));
				stockTrade.setUserName(String.valueOf(ob.get(7)));
				stockTrade.setBrokerId(String.valueOf(ob.get(8)));
				stockTrade.setBrokerName(String.valueOf(ob.get(9)));
				stockTrade.setSeatId(String.valueOf(ob.get(10)));
				stockTrade.setSeatName(String.valueOf(ob.get(11)));
				stockTrade.setTradeInOrOut(new Integer(String.valueOf(ob.get(12).toString())));
				stockTrade.setTradeFlag(new Integer(String.valueOf(ob.get(13).toString())));
				stockTrade.setTradePrice(new BigDecimal(String.valueOf(ob.get(14).toString())));
				stockTrade.setTradeDate(new Date());
				stockTrade.setShare(new Integer(String.valueOf(ob.get(15).toString())));
				stockTrade.setTurnover(new BigDecimal(String.valueOf(ob.get(16).toString())));
				stockTrade.setStampTax(new BigDecimal(String.valueOf(ob.get(17).toString())));
				stockTrade.setManagementFees(new BigDecimal(String.valueOf(ob.get(18).toString())));
				stockTrade.setTransferFee(new BigDecimal(String.valueOf(ob.get(19).toString())));
				stockTrade.setCommission(new BigDecimal(String.valueOf(ob.get(20).toString())));
				stockTrade.setBrokerage(new BigDecimal(String.valueOf(ob.get(21).toString())));
				stockTrade.setTotal(new BigDecimal(String.valueOf(ob.get(22).toString())));
				stockTrade.setSecurityInterest(new BigDecimal(String.valueOf(ob.get(23).toString())));
				stockTrade.setTradeStatus(new Integer(String.valueOf(ob.get(24).toString())));
				stockTradeList.add(stockTrade);
				System.out.println(stockTrade);
			}catch(Exception e){
				System.out.println(e);
				continue;
			}	
		}
		stockTradeDao.addStockTrade(stockTradeList);
	}

	@Override
	public boolean closeStock(String tradeStatus, String stockTradeId) {
		Integer tradeStatus1 = null;
		// 定义变量
		int rs = 0;
		// 定义数组接受前台返回的字符串数组 将,去掉
		String[] stockTradeIds = stockTradeId.split(",");
		String[] tradeStatus2 = tradeStatus.split(",");
		int num = stockTradeIds.length;
		// 将接收的数组遍历
		for (int i = 0; i < num; i++) {
			stockTradeId = stockTradeIds[i];
			tradeStatus1 = Integer.valueOf(tradeStatus2[i]);
			
			if((int) tradeStatus1==0) {
				 StockTrade stockTrade = stockTradeDao.findStockClearingById(stockTradeId);
				rs = stockTradeDao.closeStock(1, stockTradeId);
				addCapitalTransfter(stockTrade);
				
				
			}else if((int) tradeStatus1==1) {
				rs = stockTradeDao.closeStock(0, stockTradeId);
				 StockTrade stockTrade = stockTradeDao.findStockClearingById(stockTradeId);
				
				 int delete = moneyTransfersDao.deleteMoneyTransfersById(stockTrade.getFundCode());
				
				
			}
		}
		
		
		
		if (rs == 1) {
			return true;
		}
		return false;
	}

	
	  public int addCapitalTransfter(StockTrade stockTrade) {
	  
	  MoneyTransfers moneyTransfers = new MoneyTransfers(IDGenerator.getId(), IDGenerator.getNo("GPQS"), stockTrade.getFundId(), 
									stockTrade.getFundCode(),stockTrade.getFundName() , "Q0000000001",
									"W000000000000001", "1111", stockTrade.getTotal(), new Date(), stockTrade.getTradeFlag(), "清算调拨", null);
	  
	  	return moneyTransfersDao.insertMoneyTransfers(moneyTransfers);
	  
	  }
	 
	
	

}
